'##############
'Door Script
'##############

'Controls Doors

'(c) 2007-2009 Rüdiger Hülsmann
'(c) 2007-2009 Marcel Kuhnt

'Script Version: 1.1
'Omsi release: 1.0

'Needs:
'- elec

'Revision History:
'- Marcel Kuhnt		13.06.2009	Batterietrennschalter => elec_busbar_main
'- Marcel Kuhnt		21.06.2009	AI-Unterstützung hinzugefügt	
'- Rüdiger Hülsmann	24.10.2009	Ansteuerung des Kinderwagenwunsches modifiziert
'- Rüdiger Hülsmann	19.12.2009	Timer für Ausstiegstür jetzt Timegap-basiert, Bugfix: Tür schließt jetzt nur bei elec_busbar=1
'- RÜdiger Hülsmann	22.09.2010	Door light trigger value set to 0.1
'- RÜdiger Hülsmann	07.11.2010	Door speed based on reservoir pressure, air-operated doors made an option
'- Marcel Kuhnt		08.11.2010	AI procedure closing doors debugged
'- Rüdiger Hülsmann	21.11.2010	Front doors only operable when busbar on
'- Rüdiger Hülsmann	23.12.2010	External front door operation included, option: doors only operable with actual stop brake
'- Rüdiger Hülsmann	31.12.2010	Electric door function debugged
'- Rüdiger Hülsmann	05.01.2011	20h-switch
'- Rüdiger Hülsmann	07.01.2011	Backdoor manual switch sound debugged, 20h switch depending on bus type
'- Rüdiger Hülsmann	19.01.2011	Electric door support

'--------------------------------------------------------------------------------

{trigger:bus_doorfront0}
	(L.L.elec_busbar_main)
	(L.L.Velocity) 0.5 < &&
	(L.L.Nothahn_1_sw) 0 = &&
	{if}
		(M.L.trg_bus_doorfront0)
		(M.L.trg_bus_doorfront1)
	{endif}
	1 (S.L.cockpit_tuertaster1)
{end}

{trigger:bus_doorfront0_off}
	0 (S.L.cockpit_tuertaster1)
{end}

{trigger:Nothahn_1}
	(L.L.Nothahn_1_sw) ! (S.L.Nothahn_1_sw)
	(T.L.ev_doornothahn_front)
{end}

{trigger:Nothahn_2}
	(L.L.Nothahn_2_sw) ! (S.L.Nothahn_2_sw)
	(T.L.ev_doornothahn_back1)
{end}

{trigger:Nothahn_3}
	(L.L.Nothahn_3_sw) ! (S.L.Nothahn_3_sw)
	(T.L.ev_doornothahn_back2)
{end}

{trigger:Door_0_drag}
	(L.L.Nothahn_1_sw)
	{if}
		(L.S.mouse_x) 1000 / (L.L.door_0) + -1 max 1 min (S.L.door_0) s1
	{endif}
{end}

{trigger:Door_1_drag}
	(L.L.Nothahn_1_sw)
	{if}
		(L.S.mouse_x) 1000 / (L.L.door_1) + -1 max 1 min (S.L.door_1) s1
	{endif}
{end}

{trigger:Door_2_drag}
	(L.L.Nothahn_2_sw)
	{if}
		(L.S.mouse_x) 1000 / (L.L.door_2) + -1 max 1 min (S.L.door_2) s1
	{endif}
{end}


{trigger:Door_3_drag}
	(L.L.Nothahn_2_sw)
	{if}
		(L.S.mouse_x) 1000 / (L.L.door_3) + -1 max 1 min (S.L.door_3) s1
	{endif}
{end}

{trigger:Door_4_drag}
	(L.L.Nothahn_3_sw)
	{if}
		(L.S.mouse_x) 1000 / (L.L.door_4) + -1 max 1 min (S.L.door_4) s1
	{endif}
{end}


{trigger:Door_5_drag}
	(L.L.Nothahn_3_sw)
	{if}
		(L.S.mouse_x) 1000 / (L.L.door_5) + -1 max 1 min (S.L.door_5) s1
	{endif}
{end}

{trigger:bus_doorfront0_external}
	(L.L.elec_busbar_avail)
	(L.L.Nothahn_1_sw) 0 = &&
	{if}
		(M.L.trg_bus_doorfront0)
	{endif}
{end}

{trigger:cp_Tuerfluegelsperre_drag}
	(L.S.mouse_y) 20 / (L.L.cp_Tuerfluegelsperre_sw) + -1 max 1 min (S.L.cp_Tuerfluegelsperre_sw) s1

'	Stufen:
'	-1: Flügel hinten gesperrt
'	 0: kein Flügel gesperrt
'	 1: Flügel vorne gesperrt

	l1 -0.5 <
	{if}
		-1 (S.L.cp_Tuerfluegelsperre_modus) 
	{else} 
		l1 0.5 <
		{if}
			0 (S.L.cp_Tuerfluegelsperre_modus) 
		{else}
			1 (S.L.cp_Tuerfluegelsperre_modus)
		{endif}
	{endif}
{end}

{trigger:cp_Tuerfluegelsperre_off}
	(L.L.cp_Tuerfluegelsperre_modus) (S.L.cp_Tuerfluegelsperre_sw)
{end}

{trigger:bus_doorselect_F1}
	1 (S.L.cp_Tuerfluegelsperre_modus)
	(L.L.cp_Tuerfluegelsperre_modus) (S.L.cp_Tuerfluegelsperre_sw)
{end}

{trigger:bus_doorselect_F12}
	0 (S.L.cp_Tuerfluegelsperre_modus)
	(L.L.cp_Tuerfluegelsperre_modus) (S.L.cp_Tuerfluegelsperre_sw)
{end}

{trigger:bus_doorselect_F2}
	-1 (S.L.cp_Tuerfluegelsperre_modus)
	(L.L.cp_Tuerfluegelsperre_modus) (S.L.cp_Tuerfluegelsperre_sw)
{end}

{trigger:bus_doorfront1}
	(L.L.elec_busbar_main)
	(L.L.Velocity) 0.5 < &&
	(L.L.Nothahn_2_sw) 0 = &&
	{if}
		(M.L.trg_bus_doorback2)
		(M.L.trg_bus_doorback3)
	{endif}
	1 (S.L.cockpit_tuertaster2)
{end}

{trigger:bus_doorfront1_off}
	0 (S.L.cockpit_tuertaster2)
{end}

{trigger:bus_doorfront3}
	(L.L.elec_busbar_main)
	(L.L.Velocity) 0.5 < &&
	(L.L.Nothahn_3_sw) 0 = &&
	{if}
		(M.L.trg_bus_doorback4)
		(M.L.trg_bus_doorback5)
	{endif}
	1 (S.L.cockpit_tuertaster3)
{end}

{trigger:bus_doorfront3_off}
	0 (S.L.cockpit_tuertaster3)
{end}

{trigger:bus_dooraft}
	(M.L.trg_bus_dooraft)
{end}

{trigger:bus_20h-switch}
	(C.L.cockpit_has_20h_switch)
	{if}
		(M.L.trg_bus_20h-switch)
	{endif}
{end}

{trigger:int_haltewunsch}
	1 (S.L.haltewunsch)
{end}

{trigger:door_haltewunsch}
	1 (S.L.haltewunsch) (S.L.door_haltewunsch_knopfdruck)
{end}

{trigger:door_haltewunsch_off}
	0 (S.L.door_haltewunsch_knopfdruck)
{end}

{trigger:cp_schalter_kinderwagen}
	(L.L.door_handsteuerung) ! (S.L.door_handsteuerung)
	{if}
		0 (S.L.door_kinderwagenwunsch)
		(T.L.ev_kippschalter_ein)
	{else}
		(T.L.ev_kippschalter_aus)
	{endif}
		
{end}

{trigger:door_kinderwagenwunsch}
	(L.L.bremse_halte) (L.L.door_handsteuerung) ! &&
	{if}
		1 (S.L.door_kinderwagenwunsch)
	{endif}
{end}


{macro:Door_Init}
	1000 random 500 / 1 - (C.L.door_acc_var) * (C.L.fdoor_acc) + (S.L.doorAcc_0)
	1000 random 500 / 1 - (C.L.door_acc_var) * (C.L.fdoor_acc) + (S.L.doorAcc_1)
	1000 random 500 / 1 - (C.L.door_acc_var) * (C.L.bdoor_acc) + (S.L.doorAcc_2)
	1000 random 500 / 1 - (C.L.door_acc_var) * (C.L.bdoor_acc) + (S.L.doorAcc_3)
	1000 random 500 / 1 - (C.L.door_acc_var) * (C.L.bdoor_acc) + (S.L.doorAcc_4)
	1000 random 500 / 1 - (C.L.door_acc_var) * (C.L.bdoor_acc) + (S.L.doorAcc_5)

	1000 random 500 / 1 - (C.L.door_maxspeed_var) * (C.L.fdoor_maxspeed) + (S.L.doorMaxSpeed_0_norm)
	1000 random 500 / 1 - (C.L.door_maxspeed_var) * (C.L.fdoor_maxspeed) + (S.L.doorMaxSpeed_1_norm)
	1000 random 500 / 1 - (C.L.door_maxspeed_var) * (C.L.bdoor_maxspeed) + (S.L.doorMaxSpeed_2_norm)
	1000 random 500 / 1 - (C.L.door_maxspeed_var) * (C.L.bdoor_maxspeed) + (S.L.doorMaxSpeed_3_norm)
	1000 random 500 / 1 - (C.L.door_maxspeed_var) * (C.L.bdoor_maxspeed) + (S.L.doorMaxSpeed_4_norm)
	1000 random 500 / 1 - (C.L.door_maxspeed_var) * (C.L.bdoor_maxspeed) + (S.L.doorMaxSpeed_5_norm)

	(C.L.door_refl) (S.L.doorRefl_0)
	(C.L.door_refl) (S.L.doorRefl_1)
	(C.L.door_refl) (S.L.doorRefl_2)
	(C.L.door_refl) (S.L.doorRefl_3)
	(C.L.door_refl) (S.L.doorRefl_4)
	(C.L.door_refl) (S.L.doorRefl_5)

	
{end}

{macro:Door_Frame}
	(M.L.Door0_Calc)
	(M.L.Door1_Calc)
	(M.L.Door2_Calc)
	(M.L.Door3_Calc)
	(M.L.Door4_Calc)
	(M.L.Door5_Calc)
	(M.L.DoorAftCalc)
	(M.L.Nothahn_Loopsound)

	(L.L.door_0) 0.9 > (S.L.PAX_Entry1_Open)
	(L.L.door_1) 0.9 > (S.L.PAX_Entry2_Open)
	(L.L.door_2) 0.9 > (S.L.PAX_Entry0_Open)
	(L.L.door_3) 0.9 > (S.L.PAX_Exit0_Open)
	(L.L.door_4) 0.9 > (S.L.PAX_Entry3_Open)
	(L.L.door_5) 0.9 > (S.L.PAX_Exit1_Open)

'	"Dauerfeuer"-Haltewunsch über Kinderwagenschalter (macht genau dasselbe wie der "trigger:int_haltewunsch"):
	(L.L.door_handsteuerung) (L.L.door_haltewunsch_knopfdruck) ||
	{if}
		1 (S.L.haltewunsch)
	{endif}

'	Setzen der Haltestellenbremse erst bei Stillstand des Busses:
	(L.L.bremse_halte_sw) (L.L.door_20h_sw) ||
	(L.L.bremse_halte) ! (L.L.Velocity) (C.L.Door_Bremse_Halte_MaxKMH) < && &&
	{if}
		(L.L.elec_busbar_main)
		{if}
			(T.L.ev_haltebremse_an)		
			1 (S.L.bremse_halte)
		{endif}
	{endif}

'	Türfreigabekriterium

	(L.L.bremse_halte_sw) (L.L.bremse_halte) && (S.L.door_freigabe)

'	Haltewunschlampe:
	(L.L.haltewunsch) (L.L.door_2) 0.2 < (L.L.elec_busbar_main) && &&
	{if}
		1 (S.L.haltewunschlampe)
      
		(L.L.stop_request_played) !
		(L.L.haltewunschlampe) && 
		{if}
			(T.L.stop_request)
			{endif}
		1 (S.L.stop_request_played)

		{else}
		0 (S.L.stop_request_played)
		0 (S.L.haltewunschlampe) (S.L.haltewunsch)
	{endif}

'	Abfall des Kinderwagenwunsch-Relais bei fehlender Stromversorgung:

	(L.L.elec_busbar_main) 0 = 
	{if}
		0 (S.L.door_kinderwagenwunsch)
	{endif}

'	Beleuchtung der Einstiege

	(L.L.elec_busbar_main)
	{if}
		(L.L.door_0) 0.1 > (L.L.door_1) 0.1 > || (S.L.door_light_1)
		(L.L.door_2) 0.1 > (L.L.door_3) 0.1 > || (S.L.door_light_2)
		(L.L.door_4) 0.1 > (L.L.door_5) 0.1 > || (S.L.door_light_3)
	{else}
		0 (S.L.door_light_1) (S.L.door_light_2) (S.L.door_light_3)
	{endif}

'	Druckabhängige Türgeschwindigkeit
	
	(C.L.door_druckluft)
	{if}
		(L.L.doorMaxSpeed_0_norm) (L.L.bremse_p_Tank04) 100000 - 850000 / (S.L.doorMaxSpeed_0)
		(L.L.doorMaxSpeed_1_norm) (L.L.bremse_p_Tank04) 100000 - 850000 / (S.L.doorMaxSpeed_1)
		(L.L.doorMaxSpeed_2_norm) (L.L.bremse_p_Tank04) 100000 - 850000 / (S.L.doorMaxSpeed_2)
		(L.L.doorMaxSpeed_3_norm) (L.L.bremse_p_Tank04) 100000 - 850000 / (S.L.doorMaxSpeed_3)
	{endif}
'----------------------
'	AI-Ablauf:

(L.L.AI_Scheduled_AtStation) s0
1 =
{if}
'Wenn Türen geöffnet werden sollen:
	(L.L.Velocity) (C.L.Door_Bremse_Halte_MaxKMH) <
	{if}
'Nur, wenn v < maxkmh, sonst warten
'Taster jeweils nur betätigen, falls noch nicht an
		(L.L.bremse_halte_sw) !
		{if}
			(M.L.trg_bus_dooraft)
		{endif}
		(L.L.doorTarget_0) ! (L.L.door_AI_timer) 0.3 > &&
		{if}
			(M.L.trg_bus_doorfront0)
		{endif}
		(L.L.doorTarget_1) ! (L.L.door_AI_timer) 0.6 > &&
		{if}
			(M.L.trg_bus_doorfront1)
		{endif}
	{else}
		0 (S.L.door_AI_timer)
	{endif}
	(L.L.door_AI_timer) (L.S.Timegap) + (S.L.door_AI_timer)
{else}
l0 -1 =
{if}
'Wenn Türen geschlossen werden sollen:
'Taster jeweils nur betätigen, falls noch nicht aus:
	(L.L.doorTarget_1)
	{if}
		(M.L.trg_bus_doorfront1)
		0 (S.L.door_AI_timer)
	{endif}
	(L.L.doorTarget_0) (L.L.door_AI_timer) 0.3 > &&
	{if}
		(M.L.trg_bus_doorfront0)
	{endif}
'Haltestellenbremse erst wegnehmen, wenn alle Türen zu. Dann auch erst den Bus AI-mäßig freigeben:
	(L.L.door_0) 0.1 <
	(L.L.door_1) 0.1 < &&
	(L.L.door_2) 0.1 < &&
	{if}
		(L.L.bremse_halte_sw) (L.L.door_AI_timer) 0.3 > &&
		{if}
			(M.L.trg_bus_dooraft)
		{endif}
		(L.L.bremse_halte_sw) !
		{if}
			0 (S.L.AI_Scheduled_AtStation)
		{endif}
	{else}
'Sollte eine der Türen auf sein und die Haltestellenbremse aus, dann sofort diese einlegen
		(L.L.bremse_halte_sw) !
		{if}
			(M.L.trg_bus_dooraft)
		{endif}
		(L.L.doorTarget_0) !
		(L.L.doorTarget_1) ! &&
		{if}
			0 (S.L.door_AI_timer)
		{endif}
	{endif}
	(L.L.door_AI_timer) (L.S.Timegap) + (S.L.door_AI_timer)
{endif}
{endif}


{end}

{macro:Nothahn_Loopsound}

	(L.L.elec_busbar_main) 1 =
	{if}
		(L.L.Nothahn_1_sw) 1 =
		(L.L.Nothahn_2_sw) 1 = ||
		(L.L.Nothahn_3_sw) 1 = ||
		{if}
			1 (S.L.ev_door_Nothahnwarnloop)

		{else}
			0 (S.L.ev_door_Nothahnwarnloop)
		{endif}
	{else}
		0 (S.L.ev_door_Nothahnwarnloop)
	{endif}
{end}

{macro:Door0_Calc}
	(C.L.electric_doors)
	(L.L.Nothahn_1_sw) 0 = &&
	{if}
		(L.L.doorTarget_0)
		{if}
			(L.L.door_0) (F.L.door_0_opn_speed) (S.L.doorMaxSpeed_0)
		{else}
			(L.L.door_0) (F.L.door_0_cls_speed) (S.L.doorMaxSpeed_0)
		{endif}

		(C.L.fdoor_acc) (S.L.doorAcc_0)

		(L.L.doorTarget_0) ! (L.L.doorSpeed_0) s0 abs 0.05 > (L.L.door_0) 0 > || && 
		{if}
			l0 /-/ (L.L.doorMaxSpeed_0) <
			{if}
				(L.L.doorSpeed_0) (L.L.doorAcc_0) (L.S.Timegap) * - (S.L.doorSpeed_0)
			{else}
				(L.L.doorSpeed_0) (L.L.doorAcc_0) (L.S.Timegap) * + (S.L.doorSpeed_0)
			{endif}
			(L.L.door_0) (L.L.doorSpeed_0) (L.S.Timegap) * + (S.L.door_0)
		{else}
			(L.L.doorSpeed_0) abs 0.05 > (L.L.door_0) 1 < || (L.L.doorTarget_0) &&
			{if}
				l0 (L.L.doorMaxSpeed_0) <
				{if}
					(L.L.doorSpeed_0) (L.L.doorAcc_0) (L.S.Timegap) * + (S.L.doorSpeed_0) 
				{else}
					(L.L.doorSpeed_0) (L.L.doorAcc_0) (L.S.Timegap) * - (S.L.doorSpeed_0) 
				{endif}
				(L.L.door_0) (L.L.doorSpeed_0) (L.S.Timegap) * + (S.L.door_0)
			{else}
				0 (S.L.doorSpeed_0)
			{endif}
		{endif}
	{else}
	(L.L.Nothahn_1_sw) 0 =
	{if}
		(L.L.doorTarget_0) ! (L.L.doorSpeed_0) s0 abs 0.05 > (L.L.door_0) 0 > || && 
		{if}
			l0 /-/ (L.L.doorMaxSpeed_0) <
			{if}
				(L.L.doorSpeed_0) (L.L.doorAcc_0) (L.S.Timegap) * - (S.L.doorSpeed_0)
			{endif}
			(L.L.door_0) (L.L.doorSpeed_0) (L.S.Timegap) * + (S.L.door_0)
		{else}
			(L.L.doorSpeed_0) abs 0.05 > (L.L.door_0) 1 < || (L.L.doorTarget_0) &&
			{if}
				l0 (L.L.doorMaxSpeed_0) <
				{if}
					(L.L.doorSpeed_0) (L.L.doorAcc_0) (L.S.Timegap) * + (S.L.doorSpeed_0) 
				{endif}
				(L.L.door_0) (L.L.doorSpeed_0) (L.S.Timegap) * + (S.L.door_0)
			{else}
				0 (S.L.doorSpeed_0)
			{endif}
		{endif}
	{endif}
	{endif}

	(L.L.door_0) 1 >
	{if}
		(T.L.ev_doorhitopen_0)
		1 (S.L.door_0)
		(L.L.doorSpeed_0) /-/ (L.L.doorRefl_0) * (S.L.doorSpeed_0)
	{else}
		(L.L.door_0) 0 <
		{if}
			(T.L.ev_doorhitclose_0)
			0 (S.L.door_0)
			(L.L.doorSpeed_0) /-/ (L.L.doorRefl_0) * (S.L.doorSpeed_0)
		{endif}
	{endif}
{end}

{macro:Door1_Calc}
	(C.L.electric_doors)
	(L.L.Nothahn_1_sw) 0 = &&
	{if}
		(L.L.doorTarget_1)
		{if}
			(L.L.door_1) (F.L.door_1_opn_speed) (S.L.doorMaxSpeed_1)
		{else}
			(L.L.door_1) (F.L.door_1_cls_speed) (S.L.doorMaxSpeed_1)
		{endif}

		(C.L.fdoor_acc) (S.L.doorAcc_1)

		(L.L.doorTarget_1) ! (L.L.doorSpeed_1) s0 abs 0.05 > (L.L.door_1) 0 > || && 
		{if}
			l0 /-/ (L.L.doorMaxSpeed_1) <
			{if}
				(L.L.doorSpeed_1) (L.L.doorAcc_1) (L.S.Timegap) * - (S.L.doorSpeed_1)
			{else}
				(L.L.doorSpeed_1) (L.L.doorAcc_1) (L.S.Timegap) * + (S.L.doorSpeed_1)
			{endif}
			(L.L.door_1) (L.L.doorSpeed_1) (L.S.Timegap) * + (S.L.door_1)
		{else}
			(L.L.doorSpeed_1) abs 0.05 > (L.L.door_1) 1 < || (L.L.doorTarget_1) &&
			{if}
				l0 (L.L.doorMaxSpeed_1) <
				{if}
					(L.L.doorSpeed_1) (L.L.doorAcc_1) (L.S.Timegap) * + (S.L.doorSpeed_1) 
				{else}
					(L.L.doorSpeed_1) (L.L.doorAcc_1) (L.S.Timegap) * - (S.L.doorSpeed_1) 
				{endif}
				(L.L.door_1) (L.L.doorSpeed_1) (L.S.Timegap) * + (S.L.door_1)
			{else}
				0 (S.L.doorSpeed_1)
			{endif}
		{endif}
	{else}
	(L.L.Nothahn_1_sw) 0 =
	{if}
		(L.L.doorTarget_1) ! (L.L.doorSpeed_1) s0 abs 0.05 > (L.L.door_1) 0 > || && 
		{if}
			l0 /-/ (L.L.doorMaxSpeed_1) <
			{if}
				(L.L.doorSpeed_1) (L.L.doorAcc_1) (L.S.Timegap) * - (S.L.doorSpeed_1)
			{endif}
			(L.L.door_1) (L.L.doorSpeed_1) (L.S.Timegap) * + (S.L.door_1)
		{else}
			(L.L.doorSpeed_1) abs 0.05 > (L.L.door_1) 1 < || (L.L.doorTarget_1) &&
			{if}
				l0 (L.L.doorMaxSpeed_1) <
				{if}
					(L.L.doorSpeed_1) (L.L.doorAcc_1) (L.S.Timegap) * + (S.L.doorSpeed_1) 
				{endif}
				(L.L.door_1) (L.L.doorSpeed_1) (L.S.Timegap) * + (S.L.door_1)
			{else}
				0 (S.L.doorSpeed_1)
			{endif}
		{endif}
	{endif}
	{endif}

	(L.L.door_1) 1 >
	{if}
		(T.L.ev_doorhitopen_1)
		1 (S.L.door_1)
		(L.L.doorSpeed_1) /-/ (L.L.doorRefl_1) * (S.L.doorSpeed_1)
	{else}
		(L.L.door_1) 0 <
		{if}
			(T.L.ev_doorhitclose_1)
			0 (S.L.door_1)
			(L.L.doorSpeed_1) /-/ (L.L.doorRefl_1) * (S.L.doorSpeed_1)
		{endif}
	{endif}
{end}

{macro:Door2_Calc}
	(C.L.electric_doors)
	(L.L.Nothahn_2_sw) 0 = &&
	{if}
		(L.L.doorTarget_2)
		{if}
			(L.L.door_2) (F.L.door_2_opn_speed) (S.L.doorMaxSpeed_2)
		{else}
			(L.L.door_2) (F.L.door_2_cls_speed) (S.L.doorMaxSpeed_2)
		{endif}

		(C.L.bdoor_acc) (S.L.doorAcc_2)

		(L.L.doorTarget_2) ! (L.L.doorSpeed_2) s0 abs 0.05 > (L.L.door_2) 0 > || && 
		{if}
			l0 /-/ (L.L.doorMaxSpeed_2) <
			{if}
				(L.L.doorSpeed_2) (L.L.doorAcc_2) (L.S.Timegap) * - (S.L.doorSpeed_2)
			{else}
				(L.L.doorSpeed_2) (L.L.doorAcc_2) (L.S.Timegap) * + (S.L.doorSpeed_2)
			{endif}
			(L.L.door_2) (L.L.doorSpeed_2) (L.S.Timegap) * + (S.L.door_2)
		{else}
			(L.L.doorSpeed_2) abs 0.05 > (L.L.door_2) 1 < || (L.L.doorTarget_2) &&
			{if}
				l0 (L.L.doorMaxSpeed_2) <
				{if}
					(L.L.doorSpeed_2) (L.L.doorAcc_2) (L.S.Timegap) * + (S.L.doorSpeed_2) 
				{else}
					(L.L.doorSpeed_2) (L.L.doorAcc_2) (L.S.Timegap) * - (S.L.doorSpeed_2) 
				{endif}
				(L.L.door_2) (L.L.doorSpeed_2) (L.S.Timegap) * + (S.L.door_2)
			{else}
				0 (S.L.doorSpeed_2)
			{endif}
		{endif}
	{else}
	(L.L.Nothahn_2_sw) 0 =
	{if}
		(L.L.doorTarget_2) ! (L.L.doorSpeed_2) s0 abs 0.05 > (L.L.door_2) 0 > || && 
		{if}
			l0 /-/ (L.L.doorMaxSpeed_2) <
			{if}
				(L.L.doorSpeed_2) (L.L.doorAcc_2) (L.S.Timegap) * - (S.L.doorSpeed_2)
			{endif}
			(L.L.door_2) (L.L.doorSpeed_2) (L.S.Timegap) * + (S.L.door_2)
		{else}
			(L.L.doorSpeed_2) abs 0.05 > (L.L.door_2) 1 < || (L.L.doorTarget_2) &&
			{if}
				l0 (L.L.doorMaxSpeed_2) <
				{if}
					(L.L.doorSpeed_2) (L.L.doorAcc_2) (L.S.Timegap) * + (S.L.doorSpeed_2) 
				{endif}
				(L.L.door_2) (L.L.doorSpeed_2) (L.S.Timegap) * + (S.L.door_2)
			{else}
				0 (S.L.doorSpeed_2)
			{endif}
		{endif}
	{endif}
	{endif}

	(L.L.door_2) 1 >
	{if}
		(T.L.ev_doorhitopen_2)
		1 (S.L.door_2)
		(L.L.doorSpeed_2) /-/ (L.L.doorRefl_2) * (S.L.doorSpeed_2)
	{else}
		(L.L.door_2) 0 <
		{if}
			(T.L.ev_doorhitclose_2)
			0 (S.L.door_2)
			(L.L.doorSpeed_2) /-/ (L.L.doorRefl_2) * (S.L.doorSpeed_2)
		{endif}
	{endif}
{end}

{macro:Door3_Calc}
	(C.L.electric_doors)
	(L.L.Nothahn_2_sw) 0 = &&
	{if}
		(L.L.doorTarget_3)
		{if}
			(L.L.door_3) (F.L.door_3_opn_speed) (S.L.doorMaxSpeed_3)
		{else}
			(L.L.door_3) (F.L.door_3_cls_speed) (S.L.doorMaxSpeed_3)
		{endif}

		(C.L.bdoor_acc) (S.L.doorAcc_3)

		(L.L.doorTarget_3) ! (L.L.doorSpeed_3) s0 abs 0.05 > (L.L.door_3) 0 > || && 
		{if}
			l0 /-/ (L.L.doorMaxSpeed_3) <
			{if}
				(L.L.doorSpeed_3) (L.L.doorAcc_3) (L.S.Timegap) * - (S.L.doorSpeed_3)
			{else}
				(L.L.doorSpeed_3) (L.L.doorAcc_3) (L.S.Timegap) * + (S.L.doorSpeed_3)
			{endif}
			(L.L.door_3) (L.L.doorSpeed_3) (L.S.Timegap) * + (S.L.door_3)
		{else}
			(L.L.doorSpeed_3) abs 0.05 > (L.L.door_3) 1 < || (L.L.doorTarget_3) &&
			{if}
				l0 (L.L.doorMaxSpeed_3) <
				{if}
					(L.L.doorSpeed_3) (L.L.doorAcc_3) (L.S.Timegap) * + (S.L.doorSpeed_3) 
				{else}
					(L.L.doorSpeed_3) (L.L.doorAcc_3) (L.S.Timegap) * - (S.L.doorSpeed_3) 
				{endif}
				(L.L.door_3) (L.L.doorSpeed_3) (L.S.Timegap) * + (S.L.door_3)
			{else}
				0 (S.L.doorSpeed_3)
			{endif}
		{endif}
	{else}
	(L.L.Nothahn_2_sw) 0 =
	{if}
		(L.L.doorTarget_3) ! (L.L.doorSpeed_3) s0 abs 0.05 > (L.L.door_3) 0 > || && 
		{if}
			l0 /-/ (L.L.doorMaxSpeed_3) <
			{if}
				(L.L.doorSpeed_3) (L.L.doorAcc_3) (L.S.Timegap) * - (S.L.doorSpeed_3)
			{endif}
			(L.L.door_3) (L.L.doorSpeed_3) (L.S.Timegap) * + (S.L.door_3)
		{else}
			(L.L.doorSpeed_3) abs 0.05 > (L.L.door_3) 1 < || (L.L.doorTarget_3) &&
			{if}
				l0 (L.L.doorMaxSpeed_3) <
				{if}
					(L.L.doorSpeed_3) (L.L.doorAcc_3) (L.S.Timegap) * + (S.L.doorSpeed_3) 
				{endif}
				(L.L.door_3) (L.L.doorSpeed_3) (L.S.Timegap) * + (S.L.door_3)
			{else}
				0 (S.L.doorSpeed_3)
			{endif}
		{endif}
	{endif}
	{endif}

	(L.L.door_3) 1 >
	{if}
		(T.L.ev_doorhitopen_3)
		1 (S.L.door_3)
		(L.L.doorSpeed_3) /-/ (L.L.doorRefl_3) * (S.L.doorSpeed_3)
	{else}
		(L.L.door_3) 0 <
		{if}
			(T.L.ev_doorhitclose_3)
			0 (S.L.door_3)
			(L.L.doorSpeed_3) /-/ (L.L.doorRefl_3) * (S.L.doorSpeed_3)
		{endif}
	{endif}

{end}

{macro:Door4_Calc}
	(C.L.electric_doors)
	(L.L.Nothahn_3_sw) 0 = &&
	{if}
		(L.L.doorTarget_4)
		{if}
			(L.L.door_4) (F.L.door_4_opn_speed) (S.L.doorMaxSpeed_4)
		{else}
			(L.L.door_4) (F.L.door_4_cls_speed) (S.L.doorMaxSpeed_4)
		{endif}

		(C.L.bdoor_acc) (S.L.doorAcc_4)

		(L.L.doorTarget_4) ! (L.L.doorSpeed_4) s0 abs 0.05 > (L.L.door_4) 0 > || && 
		{if}
			l0 /-/ (L.L.doorMaxSpeed_4) <
			{if}
				(L.L.doorSpeed_4) (L.L.doorAcc_4) (L.S.Timegap) * - (S.L.doorSpeed_4)
			{else}
				(L.L.doorSpeed_4) (L.L.doorAcc_4) (L.S.Timegap) * + (S.L.doorSpeed_4)
			{endif}
			(L.L.door_4) (L.L.doorSpeed_4) (L.S.Timegap) * + (S.L.door_4)
		{else}
			(L.L.doorSpeed_4) abs 0.05 > (L.L.door_4) 1 < || (L.L.doorTarget_4) &&
			{if}
				l0 (L.L.doorMaxSpeed_4) <
				{if}
					(L.L.doorSpeed_4) (L.L.doorAcc_4) (L.S.Timegap) * + (S.L.doorSpeed_4) 
				{else}
					(L.L.doorSpeed_4) (L.L.doorAcc_4) (L.S.Timegap) * - (S.L.doorSpeed_4) 
				{endif}
				(L.L.door_4) (L.L.doorSpeed_4) (L.S.Timegap) * + (S.L.door_4)
			{else}
				0 (S.L.doorSpeed_4)
			{endif}
		{endif}
	{else}
	(L.L.Nothahn_3_sw) 0 =
	{if}
		(L.L.doorTarget_4) ! (L.L.doorSpeed_4) s0 abs 0.05 > (L.L.door_4) 0 > || && 
		{if}
			l0 /-/ (L.L.doorMaxSpeed_4) <
			{if}
				(L.L.doorSpeed_4) (L.L.doorAcc_4) (L.S.Timegap) * - (S.L.doorSpeed_4)
			{endif}
			(L.L.door_4) (L.L.doorSpeed_4) (L.S.Timegap) * + (S.L.door_4)
		{else}
			(L.L.doorSpeed_4) abs 0.05 > (L.L.door_4) 1 < || (L.L.doorTarget_4) &&
			{if}
				l0 (L.L.doorMaxSpeed_4) <
				{if}
					(L.L.doorSpeed_4) (L.L.doorAcc_4) (L.S.Timegap) * + (S.L.doorSpeed_4) 
				{endif}
				(L.L.door_4) (L.L.doorSpeed_4) (L.S.Timegap) * + (S.L.door_4)
			{else}
				0 (S.L.doorSpeed_4)
			{endif}
		{endif}
	{endif}
	{endif}

	(L.L.door_4) 1 >
	{if}
		(T.L.ev_doorhitopen_4)
		1 (S.L.door_4)
		(L.L.doorSpeed_4) /-/ (L.L.doorRefl_4) * (S.L.doorSpeed_4)
	{else}
		(L.L.door_4) 0 <
		{if}
			(T.L.ev_doorhitclose_4)
			0 (S.L.door_4)
			(L.L.doorSpeed_4) /-/ (L.L.doorRefl_4) * (S.L.doorSpeed_4)
		{endif}
	{endif}
{end}

{macro:Door5_Calc}
	(C.L.electric_doors)
	(L.L.Nothahn_3_sw) 0 = &&
	{if}
		(L.L.doorTarget_5)
		{if}
			(L.L.door_5) (F.L.door_5_opn_speed) (S.L.doorMaxSpeed_5)
		{else}
			(L.L.door_5) (F.L.door_5_cls_speed) (S.L.doorMaxSpeed_5)
		{endif}

		(C.L.bdoor_acc) (S.L.doorAcc_5)

		(L.L.doorTarget_5) ! (L.L.doorSpeed_5) s0 abs 0.05 > (L.L.door_5) 0 > || && 
		{if}
			l0 /-/ (L.L.doorMaxSpeed_5) <
			{if}
				(L.L.doorSpeed_5) (L.L.doorAcc_5) (L.S.Timegap) * - (S.L.doorSpeed_5)
			{else}
				(L.L.doorSpeed_5) (L.L.doorAcc_5) (L.S.Timegap) * + (S.L.doorSpeed_5)
			{endif}
			(L.L.door_5) (L.L.doorSpeed_5) (L.S.Timegap) * + (S.L.door_5)
		{else}
			(L.L.doorSpeed_5) abs 0.05 > (L.L.door_5) 1 < || (L.L.doorTarget_5) &&
			{if}
				l0 (L.L.doorMaxSpeed_3) <
				{if}
					(L.L.doorSpeed_5) (L.L.doorAcc_5) (L.S.Timegap) * + (S.L.doorSpeed_5) 
				{else}
					(L.L.doorSpeed_5) (L.L.doorAcc_5) (L.S.Timegap) * - (S.L.doorSpeed_5) 
				{endif}
				(L.L.door_5) (L.L.doorSpeed_5) (L.S.Timegap) * + (S.L.door_5)
			{else}
				0 (S.L.doorSpeed_5)
			{endif}
		{endif}
	{else}
	(L.L.Nothahn_3_sw) 0 =
	{if}
		(L.L.doorTarget_5) ! (L.L.doorSpeed_5) s0 abs 0.05 > (L.L.door_5) 0 > || && 
		{if}
			l0 /-/ (L.L.doorMaxSpeed_5) <
			{if}
				(L.L.doorSpeed_5) (L.L.doorAcc_5) (L.S.Timegap) * - (S.L.doorSpeed_5)
			{endif}
			(L.L.door_5) (L.L.doorSpeed_5) (L.S.Timegap) * + (S.L.door_5)
		{else}
			(L.L.doorSpeed_5) abs 0.05 > (L.L.door_5) 1 < || (L.L.doorTarget_5) &&
			{if}
				l0 (L.L.doorMaxSpeed_5) <
				{if}
					(L.L.doorSpeed_5) (L.L.doorAcc_5) (L.S.Timegap) * + (S.L.doorSpeed_5) 
				{endif}
				(L.L.door_5) (L.L.doorSpeed_5) (L.S.Timegap) * + (S.L.door_5)
			{else}
				0 (S.L.doorSpeed_5)
			{endif}
		{endif}
	{endif}
	{endif}

	(L.L.door_5) 1 >
	{if}
		(T.L.ev_doorhitopen_5)
		1 (S.L.door_5)
		(L.L.doorSpeed_5) /-/ (L.L.doorRefl_5) * (S.L.doorSpeed_5)
	{else}
		(L.L.door_5) 0 <
		{if}
			(T.L.ev_doorhitclose_5)
			0 (S.L.door_5)
			(L.L.doorSpeed_5) /-/ (L.L.doorRefl_5) * (S.L.doorSpeed_5)
		{endif}
	{endif}

{end}

{macro:DoorAftCalc}

	(L.L.bremse_halte_sw) !
	(L.L.door_20h_sw) ! &&
	(L.L.door_2) 0 = &&
	(L.L.door_3) 0 = &&
	(L.L.door_4) 0 = &&
	(L.L.door_5) 0 = &&
	(L.L.elec_busbar_main) ! ||
	(L.L.bremse_halte) &&
	{if}
		0 (S.L.bremse_halte) (S.L.doorAftLastOpen)
		(T.L.ev_haltebremse_aus)
	{endif}

	(L.L.elec_busbar_main)
	{if}
		(L.L.bremse_halte)
		{if}
			(L.L.doorAftLastOpen) (L.S.Timegap) + (S.L.doorAftLastOpen)
		{endif}

		
	{endif}
{end}

{macro:trg_bus_doorfront0}

	(L.L.cp_Tuerfluegelsperre_modus) -1 =
	{if}
		(L.L.doorTarget_0) 0 = (L.L.doorTarget_1) 1 = &&
		{if}
			0 (S.L.doorTarget_1)
			(T.L.ev_doortriggerclose_1)
	(C.L.door_druckluft)
	{if}
		(L.L.bremse_p_Tank04) 100000 - 0.98 * 100000 + (S.L.bremse_p_Tank04)
	{endif}
		{else}
			(L.L.doorTarget_1) !
			{if}
				1 (S.L.doorTarget_1)
				(T.L.ev_doortriggeropen_1)
	(C.L.door_druckluft)
	{if}
		(L.L.bremse_p_Tank04) 100000 - 0.98 * 100000 + (S.L.bremse_p_Tank04)
	{endif}
			{endif}
		{endif}
		(L.L.doorTarget_0)
		{if}
			0 (S.L.doorTarget_0)
			(T.L.ev_doortriggerclose_0)
	(C.L.door_druckluft)
	{if}
		(L.L.bremse_p_Tank04) 100000 - 0.98 * 100000 + (S.L.bremse_p_Tank04)
	{endif}
		{endif}
	{else}
		(L.L.cp_Tuerfluegelsperre_modus) 0 =
		{if}
			(L.L.doorTarget_0) 1 = (L.L.doorTarget_1) 1 = &&
			{if}
				0 (S.L.doorTarget_0) (S.L.doorTarget_1)
				(T.L.ev_doortriggerclose_0)
				(C.L.door_druckluft)
				{if}
					(L.L.bremse_p_Tank04) 100000 - 0.98 * 100000 + (S.L.bremse_p_Tank04)
				{endif}
				(T.L.ev_doortriggerclose_1)
				(C.L.door_druckluft)
				{if}
					(L.L.bremse_p_Tank04) 100000 - 0.98 * 100000 + (S.L.bremse_p_Tank04)
				{endif}
			{else}
				(L.L.doorTarget_0) !
				{if}
					1 (S.L.doorTarget_0)
					(T.L.ev_doortriggeropen_0)
					(C.L.door_druckluft)
					{if}
						(L.L.bremse_p_Tank04) 100000 - 0.98 * 100000 + (S.L.bremse_p_Tank04)
					{endif}
				{endif}
				(L.L.doorTarget_1) !
				{if}
					1 (S.L.doorTarget_1)
					(T.L.ev_doortriggeropen_1)
					(C.L.door_druckluft)
					{if}
						(L.L.bremse_p_Tank04) 100000 - 0.98 * 100000 + (S.L.bremse_p_Tank04)
					{endif}
				{endif}
			{endif}
		{else}
			(L.L.doorTarget_0) 1 = (L.L.doorTarget_1) 0 = &&
			{if}
				0 (S.L.doorTarget_0)
				(T.L.ev_doortriggerclose_0)
				(C.L.door_druckluft)
				{if}
					(L.L.bremse_p_Tank04) 100000 - 0.98 * 100000 + (S.L.bremse_p_Tank04)
				{endif}
			{else}
				(L.L.doorTarget_0) !
				{if}
					1 (S.L.doorTarget_0)
					(T.L.ev_doortriggeropen_0)
					(C.L.door_druckluft)
					{if}
						(L.L.bremse_p_Tank04) 100000 - 0.98 * 100000 + (S.L.bremse_p_Tank04)
					{endif}
				{endif}
			{endif}
			(L.L.doorTarget_1)
			{if}
				0 (S.L.doorTarget_1)
				(T.L.ev_doortriggerclose_1)
				(C.L.door_druckluft)
				{if}
					(L.L.bremse_p_Tank04) 100000 - 0.98 * 100000 + (S.L.bremse_p_Tank04)
				{endif}
			{endif}
		{endif}
	{endif}

'	(L.L.doorTarget_0) ! (S.L.doorTarget_0) 
'	{if}
'		(T.L.ev_doortriggeropen_0)
'	{else}
'		(T.L.ev_doortriggerclose_0)
'	{endif}
'	(C.L.door_druckluft)
'	{if}
'		(L.L.bremse_p_Tank04) 100000 - 0.98 * 100000 + (S.L.bremse_p_Tank04)
'	{endif}
'
'	(L.L.doorTarget_1) ! (S.L.doorTarget_1) 
'	{if}
'		(T.L.ev_doortriggeropen_1)
'	{else}
'		(T.L.ev_doortriggerclose_1)
'	{endif}	
'	(C.L.door_druckluft)
'	{if}
'		(L.L.bremse_p_Tank04) 100000 - 0.98 * 100000 + (S.L.bremse_p_Tank04)
'	{endif}
{end}

{macro:trg_bus_doorfront1}
' 	Überbleibsel vom M&R-Bus, bleibt nur drin, damit keine Fehlermeldungen kommen
{end}

{macro:trg_bus_doorback2}

	(L.L.doorTarget_2) ! (S.L.doorTarget_2) 
	{if}
		(T.L.ev_doortriggeropen_2)
	{else}
		(T.L.ev_doortriggerclose_2)
	{endif}	
		(C.L.door_druckluft)
		{if}
		(L.L.bremse_p_Tank04) 100000 - 0.98 * 100000 + (S.L.bremse_p_Tank04)
		{endif}
{end}

{macro:trg_bus_doorback3}

	(L.L.doorTarget_3) ! (S.L.doorTarget_3) 
	{if}
		(T.L.ev_doortriggeropen_2)
	{else}
		(T.L.ev_doortriggerclose_2)
	{endif}	
		(C.L.door_druckluft)
		{if}
		(L.L.bremse_p_Tank04) 100000 - 0.98 * 100000 + (S.L.bremse_p_Tank04)
		{endif}
{end}

{macro:trg_bus_doorback4}

	(L.L.doorTarget_4) ! (S.L.doorTarget_4) 
	{if}
		(T.L.ev_doortriggeropen_3)
	{else}
		(T.L.ev_doortriggerclose_3)
	{endif}	
		(C.L.door_druckluft)
		{if}
		(L.L.bremse_p_Tank04) 100000 - 0.98 * 100000 + (S.L.bremse_p_Tank04)
		{endif}
{end}

{macro:trg_bus_doorback5}

	(L.L.doorTarget_5) ! (S.L.doorTarget_5) 
	{if}
		(T.L.ev_doortriggeropen_3)
	{else}
		(T.L.ev_doortriggerclose_3)
	{endif}	
		(C.L.door_druckluft)
		{if}
		(L.L.bremse_p_Tank04) 100000 - 0.98 * 100000 + (S.L.bremse_p_Tank04)
		{endif}
{end}

{macro:trg_bus_dooraft}
	(L.L.bremse_halte_sw) ! (S.L.bremse_halte_sw)
	
	(L.L.bremse_halte_sw)
	{if}
		(T.L.ev_haltebremse_sw_an)
	{else}
		(T.L.ev_haltebremse_sw_aus)
	{endif}	
{end}

{macro:trg_bus_20h-switch}
	(L.L.door_20h_sw) ! (S.L.door_20h_sw)
	
	(L.L.door_20h_sw)
	{if}
		(T.L.ev_haltebremse_sw_an)
	{else}
		(T.L.ev_haltebremse_sw_aus)
	{endif}	
{end}